package com.ruoyi.report.controller.my_home_page;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.report.domain.HanZaiEmail;
import com.ruoyi.report.domain.HanZaiEmailUserName;
import com.ruoyi.report.service.HanZaiEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User: 憨憨爱崽崽
 * Name: HanZaiMyEmailController
 * Date: 2022/3/29
 * Time: 12:39
 **/
@RestController
@RequestMapping("/my_home_page")
public class HanZaiMyEmailController extends BaseController {
    @Autowired
    private HanZaiEmailService emailService;

    /**
     * 查询接收到的邮件
     * @return
     */
    @PreAuthorize("@ss.hasPermi('my_home_page:my_email:received_email:list')")
    @RequestMapping("/my_email/list")
    public TableDataInfo list(){
        LoginUser loginUser = getLoginUser();
        HanZaiEmail emailId = new HanZaiEmail();
        emailId.setReceiveId(loginUser.getUsername());
        startPage();
        List<HanZaiEmail> emailList = emailList(emailId);
        return getDataTable(emailList);
    }

    /**
     * 查询发送的邮件
     * @return
     */
    @PreAuthorize("@ss.hasPermi('my_home_page:my_email:send_the_email:list')")
    @RequestMapping("/send_the_email/list")
    public TableDataInfo sendTheEmailList(){
        startPage();
        LoginUser loginUser = getLoginUser();
        HanZaiEmail emailId = new HanZaiEmail();
        emailId.setUserId(loginUser.getUsername());
        List<HanZaiEmail> emailList = emailList(emailId);
        return getDataTable(emailList);
    }

    /**
     * 查询除自己的所有用户
     * @return
     */
    @RequestMapping("/user_name/list")
    public TableDataInfo selectUserNameList(){
        LoginUser loginUser = getLoginUser();
        System.out.println(loginUser.getUserId());
        List<HanZaiEmailUserName> userNameList = emailService.selectUserNameList(Math.toIntExact(loginUser.getUserId()));
        for (HanZaiEmailUserName email : userNameList){
            System.out.println(email.toString());
        }
        return getDataTable(userNameList);
    }

    /**
     * 发送邮件
     * @param email
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('my_home_page:send_new_email:add')")
    @RequestMapping("/send_new_email/add")
    public AjaxResult saveEmail(@Validated @RequestBody HanZaiEmail email){
        //System.out.println(email.toString());
        LoginUser loginUser = getLoginUser();
        for (HanZaiEmailUserName userName : email.getEmailUserName()){
            email.setReceiveId(userName.getUserName());
            email.setUserId(loginUser.getUsername());
            email.setSendTime(new Date());
            email.setIsRead("未读");
            System.out.println(email);
            int saveEmail = emailService.saveEmail(email);
            if (saveEmail == 0){
                return error("发送失败");
            }
        }
        System.out.println(email);
        return success("发送成功");
    }
    //@PreAuthorize("@ss.hasPermi('/my_home_page:my_email:read_email')")
    @RequestMapping("/my_email/read_email/{emailId}")
    public AjaxResult readEmail(@PathVariable Integer emailId){
        System.out.println(emailId);
        emailService.readEmail(emailId);
        return success();
    }
    //@PreAuthorize("@ss.hasPermi('/my_home_page:my_email:delete_email')")
    @RequestMapping("/my_email/delete_email/{emailId}")
    public AjaxResult deleteEmail(@PathVariable Integer emailId){
        System.out.println(emailId);
        emailService.deleteEmail(emailId);
        return success();
    }


    private List<HanZaiEmail> emailList(HanZaiEmail emailId){
        return emailService.selectEmailList(emailId);
    }
}
